package com.study.asynctask.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

/**
 * @author flwu
 * @date 2020/3/20 10:35 PM
 * @MOTTO 保持空杯和敬畏
 */
@Service
public class AsyncService {


    @Async
    public Future<Integer> sumInt(CountDownLatch count) {
        System.out.println("任务一开始了。。。" + Thread.currentThread().getName());
        Integer base = 1;
        for(int i=0; i<100; i++) {
            base += i;
        }
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        count.countDown();
        return new AsyncResult<>(base);
    }
    @Async
    public Future<Integer> sumInt2(CountDownLatch count) {
        System.out.println("任务二开始了。。。" + Thread.currentThread().getName());
        Integer base = 2;
        for(int i=0; i<100; i++) {
            base += i;
        }
        try {
            Thread.sleep(3000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        count.countDown();
        return new AsyncResult<>(base);
    }
    @Async
    public Future<Integer> sumInt3(CountDownLatch count) {
        System.out.println("任务三开始了。。。" + Thread.currentThread().getName());
        Integer base = 3;
        for(int i=0; i<100; i++) {
            base += i;
        }
        try {
            Thread.sleep(4000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        count.countDown();
        return new AsyncResult<>(base);
    }
}
